package com.ocean4.socialmap;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.ocean4.upload.ChooseImages;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

	MapFragment mapFragment;
	LocationManager locationManager;
	GoogleMap googleMap;
	
	double lat = 14.058554;
	double lng = 108.277473;
	LatLng myLatLng;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       
    }

    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
    	mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
		googleMap = mapFragment.getMap();
		
		LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        LocationListener mlocListener = new MyLocationListener();


        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    	googleMap.setMyLocationEnabled(true);

    	getCamera(lat, lng);
    	
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.d("Test", getAddress(10.768940, 106.696381));
			}
		}).start();
    	
    }
    
 // Initiatze fuction GetCamera for use general
 	public void getCamera(Double Lat, Double Lng) {

 		CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(Lat, Lng),
 				5.0f);

 		googleMap.moveCamera(cameraUpdate);
 	}
 	
 // Get address
 	public String getAddress(double Lat, double Lng) {

 		String address = null;
 		String url = "http://maps.googleapis.com/maps/api/geocode/xml?latlng="
 				+ Lat + "," + Lng + "&sensor=false";

 		try {
 			InputStream is = ((HttpURLConnection) new URL(url).openConnection())
 					.getInputStream();
 			Document document = DocumentBuilderFactory.newInstance()
 					.newDocumentBuilder().parse(is);
 			NodeList nodeList = document
 					.getElementsByTagName("formatted_address");
 			address = nodeList.item(0).getTextContent();
 		} catch (MalformedURLException e) {

 			e.printStackTrace();
 		} catch (IOException e) {

 			e.printStackTrace();
 		} catch (SAXException e) {

 			e.printStackTrace();
 		} catch (ParserConfigurationException e) {

 			e.printStackTrace();
 		}
 		return address.replace("Vietnam", "Việt Nam");
 	}
 	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
        	Intent intent = new Intent(this, ChooseImages.class);
        	startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }
    
    
    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener

    {

    @Override

    public void onLocationChanged(Location loc)

    {

    	myLatLng = new LatLng(loc.getLatitude(),
    			loc.getLongitude());
    	 CameraPosition myPosition = new CameraPosition.Builder()
         .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
		 googleMap.animateCamera(
		     CameraUpdateFactory.newCameraPosition(myPosition));
 
    loc.getLatitude();
    loc.getLongitude();

    Log.d("Test", "My current location is: " +

    "Latitud = " + loc.getLatitude() +

    "Longitud = " + loc.getLongitude());
    String Text = "My current location is: " +

    "Latitud = " + loc.getLatitude() +

    "Longitud = " + loc.getLongitude();

    
    Toast.makeText( getApplicationContext(),

    Text,

    Toast.LENGTH_SHORT).show();

    }


    @Override

    public void onProviderDisabled(String provider)

    {

    Toast.makeText( getApplicationContext(),

    "Gps Disabled",

    Toast.LENGTH_SHORT ).show();

    }


    @Override

    public void onProviderEnabled(String provider)

    {

    Toast.makeText( getApplicationContext(),

    "Gps Enabled",

    Toast.LENGTH_SHORT).show();

    }


    @Override

    public void onStatusChanged(String provider, int status, Bundle extras)

    {


    }

    }/* End of Class MyLocationListener */
}
